﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;

namespace ttg_internal.Utilities.UsageVisualizer.Converter
{
    [ValueConversion(typeof(double), typeof(double))]
    internal class MultiplyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TypeConverter doubleTypeConverter = TypeDescriptor.GetConverter(typeof(double));
            Func<object, double> doubleConverter = d =>
                                                       {
                                                           if (d is double)
                                                           {
                                                               return (double)d;
                                                           }
                                                           if (d is string)
                                                           {
                                                               return (double)doubleTypeConverter.ConvertFromInvariantString((string)d);
                                                           }
                                                           return (double)doubleTypeConverter.ConvertFrom(d);
                                                       };

            return doubleConverter(value) * doubleConverter(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
